#!/bin/sh
#
# CA - wrapper around ca to make it easier to use ... basically ca requires
#      some setup stuff to be done before you can use it and this makes
#      things easier between now and when Eric is convinced to fix it :-)
#
# CA -newca ... will setup the right stuff
# CA -newreq ... will generate a certificate request 
# CA -sign ... will sign the generated request and output 
#
# At the end of that grab newreq.pem and newcert.pem (one has the key 
# and the other the certificate) and cat them together and that is what
# you want/need ... I'll make even this a little cleaner later.
#
#
# 12-Jan-96 tjh    Added more things ... including CA -signcert which
#                  converts a certificate to a request and then signs it.
# 10-Jan-96 eay    Fixed a few more bugs and added the SSLEAY_CONFIG
#		   environment variable so this can be driven from
#		   a script.
# 25-Jul-96 eay    Cleaned up filenames some more.
# 11-Jun-96 eay    Fixed a few filename missmatches.
# 03-May-96 eay    Modified to use 'ssleay cmd' instead of 'cmd'.
# 18-Apr-96 tjh    Original hacking
#
# Tim Hudson
# tjh@cryptsoft.com
#

# default openssl.cnf file has setup as per the following
# demoCA ... where everything is stored

if [ -z "$OPENSSL" ]; then OPENSSL=openssl; fi

DAYS="-days 3650"	# 1 year
CADAYS="-days 3650"	# 3 years
REQ="$OPENSSL req $SSLEAY_CONFIG"
CA="$OPENSSL ca $SSLEAY_CONFIG"
VERIFY="$OPENSSL verify"
X509="$OPENSSL x509"

BASE=`pwd`
CONFIG=$BASE/serverJES.cnf
CATOP=$BASE/CA
CAKEY=cakey.pem
CAREQ=careq.pem
CACERT=cacert.pem

# create a certificate request
$REQ -config $CONFIG -new -nodes -keyout $CATOP/certs/JESServerKey.pem -out $CATOP/certs/JESServerReq.pem $DAYS
echo "Request is in $CATOP/certs/JESServerReq.pem, private key is in $CATOP/certs/JESServerKey.pem"
$CA -config $CONFIG -policy policy_anything -cert $CATOP/$CACERT -keyfile $CATOP/private/$CAKEY -out $CATOP/certs/SignedJESServerReq.pem -in $CATOP/certs/JESServerReq.pem $DAYS
RET=$?
echo "Signed certificate is in $CATOP/certs/SignedJESServerReq.pem"
exit $RET

